package com.jjb.www.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cqjjb.calendarview.Calendar;
import com.cqjjb.calendarview.CalendarLayout;
import com.cqjjb.calendarview.CalendarView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.action.StatusAction;
import com.jjb.www.app.AppActivity;
import com.jjb.www.http.api.AgencyListApi;
import com.jjb.www.http.api.ClockLogDateListApi;
import com.jjb.www.http.api.ClockLogDetailV2Api;
import com.jjb.www.http.glide.GlideApp;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.adapter.DayAdapter;
import com.jjb.www.widget.StatusLayout;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Author: crg
 * time:   2021/9/23 9:58
 * des:    打卡记录
 */
public class PunchClockRecordActivity extends AppActivity implements
        CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener,
        StatusAction {
    private CalendarLayout mCalendarLayout;
    private CalendarView mCalendarView;
    private StatusLayout mHlPunchHint;
    private RecyclerView mRvClockRecord;
    private ShapeTextView mTvTime;
    private ShapeTextView mTvDay;
    private String mech_id;
    private String mech_training_sentry_id;
    private String mech_training_plan_id;
    private String student_id;
    private String yearsData;
    private String mDay;
    private ShapeImageView mImgHead;
    private ShapeTextView mTvName;
    private ShapeTextView mTvDataTime;
    private DayAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_punch_clock_record;
    }

    @Override
    protected void initView() {
        mTvDay = findViewById(R.id.tv_day);
        mTvTime = findViewById(R.id.tv_time);
        mImgHead = findViewById(R.id.img_head);
        mTvName = findViewById(R.id.tv_name);
        mTvDataTime = findViewById(R.id.tv_data_time);
        mCalendarLayout = findViewById(R.id.calendarLayout);
        mCalendarView = findViewById(R.id.calendarView);
        mHlPunchHint = findViewById(R.id.hl_punch_hint);
        mRvClockRecord = findViewById(R.id.rv_clock_record);
        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnCalendarSelectListener(this);
        setOnClickListener(mTvDay);
    }

    @Override
    protected void initData() {
        mech_id = getString("mech_id");
        mech_training_sentry_id = getString("mech_training_sentry_id");
        mech_training_plan_id = getString("mech_training_plan_id");
        student_id = getString("student_id");

        yearsData = mCalendarView.getCurYear() + "-" + mCalendarView.getCurMonth();
        mTvTime.setText(String.format(Locale.CHINA, "%d年", mCalendarView.getCurYear()) + String.format(Locale.CHINA, "%d月", mCalendarView.getCurMonth()));
        mDay =getDay();
        getPunchListData(yearsData);
        getDataList(mDay);
    }

    private String getDay() {
        long timeGetTime = new Date().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sdf.format(timeGetTime);
    }
    private void getPunchListData(String yearsData) {
        EasyHttp.get(this)
                .api(new ClockLogDateListApi()
                        .setStudent_id(student_id)
                        .setMech_id(mech_id)
                        .setMech_training_plan_id(mech_training_plan_id)
                        .setMech_training_sentry_id(mech_training_sentry_id)
                        .setYears(yearsData))
                .request(new HttpCallback<HttpData<ClockLogDateListApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<ClockLogDateListApi.Bean> data) {
                        GlideApp.with(getContext())
                                .load(data.getData().getUser_info().getImg())
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into(mImgHead);
                        mTvName.setText(data.getData().getUser_info().getNickname());
                        mTvDataTime.setText(data.getData().getUser_info().getClock_date());
                    }
                });
    }

    @Override
    public void onClick(View view) {
        if (view == mTvDay) {
            mCalendarView.scrollToCurrent();
        }
    }

    @Override
    public StatusLayout getStatusLayout() {
        return mHlPunchHint;
    }

    @Override
    public void onYearChange(int year) {

    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        long timeGetTime = calendar.getTimeInMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        yearsData = calendar.getYear() + "-" + calendar.getMonth();
        mDay = sdf.format(timeGetTime);
        if (isClick) {
            getDataList(mDay);
        } else {
            getPunchListData(yearsData);
        }

        mTvTime.setText(String.format(Locale.CHINA, "%d年", calendar.getYear()) + String.format(Locale.CHINA, "%d月", calendar.getMonth()));
    }

    private void getDataList(String day) {
        EasyHttp.get(this)
                .api(new ClockLogDetailV2Api()
                        .setStudent_id(student_id)
                        .setMech_id(mech_id)
                        .setMech_training_plan_id(mech_training_plan_id)
                        .setMech_training_sentry_id(mech_training_sentry_id)
                        .setDay(day)
                        .setLat("")
                        .setLng(""))
                .request(new HttpCallback<HttpData<ClockLogDetailV2Api.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<ClockLogDetailV2Api.Bean> data) {

                    }
                });
    }
}